#include "http_service.h"

namespace DOD {




std::int32_t HttpService::GenOldDownloadListFile()
{
	if (!helpers::Directory::TryCreateDirectories(kOldDownloadListFilename.c_str()))
	{
		PRINTF_ERROR("Failed to create list file directory\n");
		return DE_UNDEFINED;
	}

	int ret = std::remove(kOldDownloadListFilename.c_str());
	if (-1 == ret)
	{
		if (errno == EACCES)
		{
			PRINTF_ERROR("Failed to delete old list file\n");
			return DE_UNDEFINED;
		}
	}
	ret = std::rename(kNewDownloadListFilename.c_str(), kOldDownloadListFilename.c_str());
	if (-1 == ret)
	{
		if (errno == ENOENT)
		{
			FILE* p = fopen(kOldDownloadListFilename.c_str(), "w+");
			if (NULL == p)
			{
				PRINTF_ERROR("Failed to create old list file\n");
				return DE_UNDEFINED;
			}
			fclose(p);
		} 
		else
		{
			PRINTF_ERROR("Failed to rename list file to old\n");
			return DE_UNDEFINED;
		}
	}

	return DE_OK;
}

std::int32_t HttpService::InitAppConfig(std::string config_file)
{
	if (DE_OK != TheAppConfig().InitOldConfig(config_file)) {
		PRINTF_ERROR("Failed to init download config\n");
		return DE_UNDEFINED;
	}
	return DE_OK;
}

}